package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"work/api/basic/config"
	"work/api/handler/Request"
	__ "work/user-srv/basic/proto"
	"work/user-srv/pkg"
)

func CreateUser(c *gin.Context) {

	var form Request.UserRequest
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	user, err := config.UserClient.CreateUser(c, &__.CreateUserReq{
		UserName: form.Username,
		Password: form.Password,
	})
	if err != nil {
		return
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(user.UserId, 10))
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"error": err,
		"token": handler,
		"code":  200,
	})

}
